package lg.minecraft.plugin.worldprotect.listener;

import lg.minecraft.plugin.worldprotect.LGWorldProtect;
import lg.minecraft.plugin.worldprotect.WorldSettingType;
import lg.minecraft.plugin.worldprotect.util.WorldSettings;
import org.bukkit.World;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.*;
import org.bukkit.event.vehicle.VehicleCreateEvent;

/**
 * @author langle__
 * @version 1.0
 */
public class EntityEvent implements Listener {

    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
        if (event.isCancelled()) return;
        Entity entity = event.getEntity();
        Entity damager = event.getDamager();
        World world = damager.getWorld();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);

        if (worldSettings.isEnabled(WorldSettingType.PVP) && entity instanceof Player) {
            if (damager instanceof Player) {
                event.setCancelled(true);
                if (LGWorldProtect.getMainConfig().isEnableTip()) damager.sendMessage(LGWorldProtect.getLanguage().getWorldSettingTip(WorldSettingType.PVP));
            } else if (damager instanceof Projectile) {
                Projectile projectile = (Projectile) damager;
                if (projectile.getShooter() instanceof Player) {
                    event.setCancelled(true);
                    if (LGWorldProtect.getMainConfig().isEnableTip()) damager.sendMessage(LGWorldProtect.getLanguage().getWorldSettingTip(WorldSettingType.PVP));
                }
            }
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityExplode(EntityExplodeEvent event) {
        if (event.isCancelled()) return;
        Entity entity = event.getEntity();
        World world = entity.getWorld();
        EntityType entityType = event.getEntityType();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);

        if (worldSettings.isEnabled(WorldSettingType.EXPLOSION)) {
            event.setCancelled(true);
        } else if (entityType == EntityType.PRIMED_TNT && worldSettings.isEnabled(WorldSettingType.TNT)) {
            event.setCancelled(true);
        } else if (entityType == EntityType.PRIMED_TNT && worldSettings.isEnabled(WorldSettingType.CREEPER)) {
            event.setCancelled(true);
        } else if (entityType == EntityType.WITHER_SKULL && worldSettings.isEnabled(WorldSettingType.WITHER_DESTRUCTION)) {
            event.setCancelled(true);
        } else if (entityType == EntityType.ENDER_DRAGON && worldSettings.isEnabled(WorldSettingType.DRAGON_GRIEF)) {
            event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityChangeBlock(EntityChangeBlockEvent event) {
        if (event.isCancelled()) return;
        Entity entity = event.getEntity();
        World world = entity.getWorld();
        EntityType entityType = event.getEntityType();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);

        if (entityType == EntityType.WITHER && worldSettings.isEnabled(WorldSettingType.WITHER_DESTRUCTION)) {
            event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityPickupItem(EntityPickupItemEvent event) {
        if (event.isCancelled()) return;
        Entity entity = event.getEntity();
        World world = entity.getWorld();

        if (entity.getType() == EntityType.PLAYER && entity.isOp()) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);

        if (worldSettings.isEnabled(WorldSettingType.ITEM_PICKUP)) {
            event.setCancelled(true);
        }

    }

    @EventHandler
    public void onFoodLevelChange(FoodLevelChangeEvent event) {
        if (event.isCancelled()) return;
        HumanEntity entity = event.getEntity();

        if (event.getFoodLevel() >= 20) return;
        if (!(entity instanceof Player)) return;

        Player player = (Player) entity;
//        player.setSaturation(20.0F);
//        player.setExhaustion(0.0F);
//        event.setFoodLevel(20);
//        event.setCancelled(true);
    }

    @EventHandler
    public void onFoodLevelChange(VehicleCreateEvent event) {
        if (event.isCancelled()) return;
        Vehicle vehicle = event.getVehicle();
    }

}
